package com.example.demo.service;

import com.example.demo.bean.Comment;
import com.example.demo.bean.Indent;
import com.example.demo.bean.User;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by yanmaoyuan on 2018/4/16.
 */
@Service
public class UserService {

    @Autowired
    private UserRepository userRepository = null;

    @Autowired
    private IndentService indentService;

    @Autowired
    private CommentService commentService;

    @javax.transaction.Transactional
    public void save(User user){
        userRepository.save(user);
    }

    @Transactional
    public User getUserById(String userId) {
        User user =  userRepository.findUserByUserId(userId);
        List<Indent> indents = indentService.getIndentByUserAndStatus(userId, "已完成");
        int total = indents.size();
        List<Comment> comments = commentService.getGoodComment(userId);
        int good = comments.size();
        double rate = 0.0;
        int rateInt = 0;
        if(total != 0){
            rate = ((double)good/(double)total) * 100;
            rateInt = (int) rate;
        }else{
        }
        user.setGoodRate(rateInt);
        return user;
    }

    @Transactional
    public void addUser(User user) {
        userRepository.save(user);
    }

//    @Transactional
//    public User getUserByWechat(String wechatId) { return userRepository.getByWechatIdEquals(wechatId); }

    @Transactional
    public User getUserByStudentId(String studentId) { return userRepository.getByStudentIdEquals(studentId); }

}
